package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);

//        String url="http://localhost:8081/user/"+order.getUserId();

        //spring会自动帮助我们从eureka-server端，根据userservice这个服务名称，获取实例列表，而后完成负载均衡。
//        String url = "http://userservice/user/"+order.getUserId();
//        //发起调用
//        User user=restTemplate.getForObject(url, User.class);

        //利用Feign发起http请求，查询用户
        User user = userClient.findById(order.getUserId());
        //存入order
        order.setUser(user);
        // 4.返回
        return order;
    }
}
